package b3.widget.home.bottom
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.tab.*;
	import b3.widget.warehouse.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class BottomPane extends TabController
	{
		public static const STATUS_HIDE:int = 0;
		public static const STATUS_EXTEND:int = 1;
		public static const STATUS_NONE:int = 2;
		
		//记录当前伸缩的状态
		public var extendFlag:int;
		//包裹图标
		public var caseIcon:BasicIcon;
		private var upArrow:Sprite;
		private var downArrow:Sprite;
		private static var instance:BottomPane;

		public function BottomPane()
		{
			//主要图标
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("BottomNavigatorBgr");
			var FriendIcon:Class = ResourceHelper.getInstance().getClassByName("FriendIcon");
			var BuildIcon:Class = ResourceHelper.getInstance().getClassByName("BuildIcon");
			var WeaponIcon:Class = ResourceHelper.getInstance().getClassByName("WeaponIcon");
			var GreenIcon:Class = ResourceHelper.getInstance().getClassByName("GreenIcon");
			var MiracleIcon:Class = ResourceHelper.getInstance().getClassByName("MiracleIcon");
			var CaseIcon:Class = ResourceHelper.getInstance().getClassByName("CaseIcon");
			super(TabControllerBackground);
			this.anchor = BasicWidget.ANCHOR_MIDDLE_BOTTOM;
			this.setTitleType(1);
			var UpArrow:Class = ResourceHelper.getInstance().getClassByName("BottomNavigatorUpArrow");
			upArrow = new BasicIcon(UpArrow);
			upArrow.x = 600;
			upArrow.y = 8;
			addChild(upArrow);
			upArrow.addEventListener(MouseEvent.CLICK, onUpDownArrowButtonClick);
			var DownArrow:Class = ResourceHelper.getInstance().getClassByName("BottomNavigatorDownArrow");
			downArrow = new BasicIcon(DownArrow);
			downArrow.visible = false;
			downArrow.x = upArrow.x;
			downArrow.y = upArrow.y;
			addChild(downArrow);
			downArrow.addEventListener(MouseEvent.CLICK, onUpDownArrowButtonClick);
			
			//标签页
			var tab:Tab = new Tab("好友", 5, 1, 5);
			setTabButtonPos(tab.pageSelector);
			this.addTab(FriendIcon, tab.tabName, tab);
			tab = new Tab("建筑", 5, 1, 5);
			setTabButtonPos(tab.pageSelector);
			this.addTab(BuildIcon, tab.tabName, tab);
			tab = new Tab("导弹", 5, 1, 5);
			setTabButtonPos(tab.pageSelector);
			this.addTab(WeaponIcon, tab.tabName, tab);
			tab = new Tab("绿化", 5, 1, 5);
			setTabButtonPos(tab.pageSelector);
			this.addTab(GreenIcon, tab.tabName, tab);
			tab = new Tab("奇迹", 5, 1, 5);
			setTabButtonPos(tab.pageSelector);
			this.addTab(MiracleIcon, tab.tabName, tab);
			caseIcon = new BasicIcon(CaseIcon, "包裹");
			caseIcon.x = 72 + 70 * 5;
			caseIcon.y = -5;
			caseIcon.addEventListener(MouseEvent.CLICK, onCaseIconClick);
			addChild(caseIcon);
			this.setTabTitlePos(72, -5, 70)
			//标签图标的事件（扩展自己）
			for (var i:int = 0; i < tabTitles.length; i++)
			{
				this.tabTitles[i].addEventListener(MouseEvent.CLICK, onTabIconClick);
			}
			
			PlayerManager.getInstance().me.friendList.addEventListener(ModelEvent.REFRESH_FRIEND_LIST, onRefreshFriendList);
			loadBuildingList();
			onRefreshFriendList(null);
			this.x = -this.width / 2 + 30;
		}
		
		private function onCaseIconClick(event:Event):void
		{
			var warehouse:Warehouse = Warehouse.getInstance();
			warehouse.x = -warehouse.width / 2;
			warehouse.y = -warehouse.height / 2;
			WidgetManager.getInstance().popOut([warehouse], true);
		} 
		
		private function loadBuildingList():void
		{
			var buildingConfig:BuildingConfig;
			//所有建筑
			var buildingIndexes:Array = new Array();
			for (var indexStr:String in Config.buildingConfigs)
			{
				buildingConfig = Config.buildingConfigs[indexStr];
				if (buildingConfig.sortIndex >= 0)
				{
					buildingIndexes.push(buildingConfig.index);
				}
			}
		    buildingIndexes.sort(sortObjects);
		   	
		   	var me:Player = PlayerManager.getInstance().me;
			for (var i:int = 0; i < buildingIndexes.length; i++)
			{
				var index:int = buildingIndexes[i];
				buildingConfig = Config.buildingConfigs[index];
				var buildingItem:BuildingItem = new BuildingItem(index);
				this.tabs[buildingConfig.type + 1].addItem(buildingItem, 92, 133.25);
			}
		}
		
		private function setTabButtonPos(pageSelector:TabPageSelector):void
		{
			pageSelector.hideNumArea();
			pageSelector.x = -70;
			pageSelector.y = 20;
			pageSelector.pageUp.x = 0;
			pageSelector.pageUp.y = 0;
			pageSelector.firstPage.x = 0;
			pageSelector.firstPage.y = 30;
			pageSelector.pageDown.x = 600;
			pageSelector.pageDown.y = 0;
			pageSelector.lastPage.x = 600;
			pageSelector.lastPage.y = 30;
		}
		
		private function onRefreshFriendList(event:Event):void
		{
			var friendList:FriendList = PlayerManager.getInstance().me.friendList;
			var friendTab:Tab = this.tabs[0]; 
			friendTab.removeAll();
			//中立
			var ranking:int;
			var friendItem:FriendItem;
			for (var i:int = 0; i < friendList.friendIds.length; i++)
			{
				var friend:Player = PlayerManager.getInstance().players[friendList.friendIds[i]];
				if (friend.type == PlayerManager.CENTER_TYPE)
				{
					friendItem = new FriendItem(friendList.friendIds[i], -1);
					friendTab.addItem(friendItem, 92, 112);
				}
			}
			//自己
			ranking = friendList.friendIds.indexOf(PlayerManager.getInstance().myUserId);
			friendItem = new FriendItem(PlayerManager.getInstance().myUserId, ranking);
			friendTab.addItem(friendItem, 92, 112);
			//好友
			for (i = 0; i < friendList.friendIds.length; i++)
			{
				friend = PlayerManager.getInstance().players[friendList.friendIds[i]];
				if (friend.type == PlayerManager.NORMAL_TYPE && friendList.friendIds[i] != PlayerManager.getInstance().myUserId)
				{
					ranking = friendList.friendIds.indexOf(friendList.friendIds[i]);
					friendItem = new FriendItem(friendList.friendIds[i], ranking);
					friendTab.addItem(friendItem, 92, 112);
				}
			}
			//邀请
			for (i = 0; i < 5; i++)
			{
				friendItem = new FriendItem(-1, -1);
				friendTab.addItem(friendItem, 92, 112);
			}
		}
		
		private function onUpDownArrowButtonClick(event:MouseEvent):void
		{
			switchExtendStatus();
		}
		
		public function onTabIconClick(event:MouseEvent):void
		{
			extend();
		}
		
		private function setExtendStatus(status:int):void
		{
			extendFlag = status;
		}
		
		public function extend():void
		{
			this.setTabPagePos(100, 75);
			extendFlag = STATUS_NONE;
			TweenLite.to(this, 0.5, {y: BFont.stageHeight - 205, onComplete: setExtendStatus, onCompleteParams: [STATUS_EXTEND]});
			upArrow.visible = false;
			downArrow.visible = true;
		}
		
		public function hide():void
		{
			this.setTabPagePos(100, 175);
			extendFlag = STATUS_NONE;
			TweenLite.to(this, 0.5, {y: BFont.stageHeight - 65, onComplete: setExtendStatus, onCompleteParams: [STATUS_HIDE]});
			upArrow.visible = true;
			downArrow.visible = false;
		}
		
		//转换当前的伸缩状态
		public function switchExtendStatus():void
		{
			if (extendFlag)
			{
				hide();
			}
			else
			{
				extend();
			}
		}
		
		public function refresh():void
		{
		}
		
		public static function getInstance():BottomPane
		{
			if (!instance)
				instance = new BottomPane();
			return instance;
		}
		
		//排序
		private function sortObjects(a:int, b:int):int
		{
			var objectConfigA:ObjConfig = Config.buildingConfigs[a];
			var objectConfigB:ObjConfig = Config.buildingConfigs[b];
			if (objectConfigA.sortIndex < objectConfigB.sortIndex)
			{
				return -1;
			}
			else if (objectConfigA.sortIndex > objectConfigB.sortIndex)
			{
				return 1;
			}
			return 0;
		}
	}
}